layui.use(['tableEdit', 'table', 'form', 'laydate',"selectInput","excel"], function () {
    let layer = layui.layer //弹层
        ,table = layui.table //表格
        ,form = layui.form,
        $ = layui.jquery,
        laydate = layui.laydate,
        excel = layui.excel,
        selectInput = layui.selectInput;

    lay('.starttime').each(function () {
        laydate.render({
            elem: this
        });
    });


    function getxmselect(el, name, data) {
        return xmSelect.render({
            el: el,
            name: name,
            tree: {
                show: true,
                showLine: true,
                showFolderIcon: true,
                indent: 20,
                expandedKeys: false
            },
            height: 'auto',
            data: data,
            on: function ({arr, change, isAdd}) {
                return this
            }
        })
    }
    setCustomerInfo("")
    function setCustomerInfo(initData) {
        //客户名称
        let ins = selectInput.render({
            elem: '#customerSelect',
            name: 'customerSelect',
            initValue: initData,
            hasSelectIcon: true,
            placeholder: '请输入名称',
            data: [],
        })
        //监听输入
        ins.on('itemInput(customerSelect)', function (obj) {
            let tempData = [];
            $.ajax({
                url: "basicInformation/getCustomerInfo?info=" + obj.value,
                async: false,
                success: function (res) {
                    if (res.status == 200) {
                        customerInfo = res.data.customerInfo;
                        for (let i in customerInfo) {
                            customerInfo[i]["value"] = customerInfo[i].code;
                            tempData.push(customerInfo[i]);
                        }
                    }
                }
            })
            ins.addSelect(tempData)
        })

        // 监听select 选择事件
        ins.on('itemSelect(customerSelect)', function (obj) {
            let code = obj.data;
            let name = obj.textContent;
            $("#customerCode").val(code);
            $("#customerName").val(name);
        });
    }

    let billNoStatusList=[];
    layer.ready(function () {
        //供应商
        $.ajax({
            url: "customer2/getAllInspectUnit",
            success: function (res) {
                if (res.status == 200) {
                    $.each(res.data, function (index, item) {
                        $("#supplierCode").append(new Option(item.name, item.code));
                    });
                    form.render('select');
                } else {
                    layer.msg(res.msg, {icon: 5});
                }
            }
        });
        //单据信息
        let billNoStatus = [
            {name: "草稿箱", value: "1"},
            {name: "待批箱", value: "2"},
            {name: "已批箱", value: "3"},
            {name: "完成箱", value: "4"},
            {name: "垃圾箱", value: "5"}
        ];
        billNoStatusList= getxmselect("#status", "status", billNoStatus);
    });
    form.render();//表单刷新

    let initData = [];
    table.render({
        elem: '#shipmentPlanSelect'
        , data: initData
        , toolbar: '#shipmentPlanToolbar'
        ,totalRow: true
        , cols: [[
            {field: 'numbers',width: 50,type:'numbers',title:'序号', totalRowText: '合计'}
            , {field: 'shipmentNo', title: '计划编号', sort: true, width: 150}
            , {field: 'createTimeStr', title: '制单日期', sort: true, width: 120}
            , {field: 'shipmentDateStr', title: '出运日期', sort: true, width: 120}
            , {field: 'customerName', title: '客户名称', sort: true, width: 200}
            , {field: 'businessMode', title: '业务模式', sort: true, width: 120}
            , {field: 'salesContractNo', title: '销售合同号', sort: true, width: 120}
            , {field: 'stockNo', title: '采购单号', sort: true, width: 120}
            , {field: 'supplyerName', title: '供应商名称', sort: true, width: 120}
            , {field: 'goodsCode', title: '产品编码', sort: true, width: 120}
            , {field: 'specNo', title: '规格编号', sort: true, width: 120}
            , {field: 'goodsCName', title: '中文名称', sort: true, width: 120}
            , {field: 'goodsEName', title: '英文名称', sort: true, width: 120}
            , {field: 'goodsCDesc', title: '中文描述', sort: true, width: 120}
            , {field: 'goodsEDesc', title: '英文描述', sort: true, width: 120}
            , {field: 'shipmentPlanNum', title: '出运计划数量', sort: true, width: 120, totalRow: true}
            , {field: 'carryPlanProductNum', title: '出运明细数量', sort: true, width: 120, totalRow: true}
            , {field: 'unCarryPlanProductNum', title: '未出运明细数量', sort: true, width: 120, totalRow: true}
            , {field: 'billExchangeInfoNum', title: '结汇数量', sort: true, width: 120, totalRow: true}
            , {field: 'unBillExchangeInfoNum', title: '未结汇数量', sort: true, width: 120, totalRow: true}
            , {field: 'productUnit', title: '产品单位', sort: true, width: 120}
            , {field: 'productType', title: '产品类型', sort: true, width: 120}
            // , {field: 'costPrice', title: '成本单价', sort: true, width: 120, totalRow: true}
            , {field: 'salesmanName', title: '业务员', sort: true, width: 120}
            , {field: 'executorName', title: '操作员', sort: true, width: 120}
        ]]
        , id: 'shipmentPlanId'
        , height: $(document).height() - $('#shipmentPlanSelect').offset().top - 50
        , page: true
        , limits: [300, 600, 900]
    });


    form.on('submit(submitform)', function (data) {
        table.reload('shipmentPlanId', {
            page: {
                limit:300,
                curr: 1
            }
            , url: 'shipmentPlan/shipmentPlanStatistics1ListByPage'
            , where: data.field
        }, 'data');
        return false;
    });


    form.on('submit(exportData)', function (queryReq) {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            data: queryReq.field,
            url:"shipmentPlan/shipmentPlanStatistics1ListExport",
            success: function(res) {
                if(res.status == 200){
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 80,
                        'X': 80
                    }, 80);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '出运结汇一览总表.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                }else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });

    document.getElementById('clear').onclick = function(){
        billNoStatusList.setValue([ ]);
    };

});


